Skip to content

feat(import): keep the diagram layout when re-importing a B-UML file (Refs #774) - #778

Merged
ArmenSl merged 3 commits into
feature/migration-react-flow-syncfrom
fix/774-layout-preserving-import
Nov 12, 2026
Merged

ArmenSl merged 3 commits into
feature/migration-react-flow-syncfrom
fix/774-layout-preserving-import

Conversation

@ArmenSl

@ArmenSl ArmenSl commented Nov 10, 2026 •

Copy link
Copy Markdown
Collaborator

Refs #774. Backend first step (option A of the analysis): re-importing a B-UML .py file can keep the layout of the diagram it updates. The editor part follows in a second PR.

What changes

  • services/converters/layout_adoption.py (new): adopt_layout(existing, imported) -> (model, report) over the v4 wire shape (nodes / edges, position relative to parentId, data.points on edges).
    • Nodes are matched by id when the import kept it, otherwise by type and name (case-insensitive) inside the matched parent, duplicates in order. A matched node keeps the existing id, position and size. The size grows if its new content needs more room.
    • Attribute, method and state-body rows and agent components (intents with their bodies, LLMs, ...) keep their existing ids, matched by name inside their match. References to the remapped ids, such as targetMethodId, follow them.
    • Renames: a renamed node keeps its id and place only when it is the one unmatched node of its type and parent, on each side, with the same edges to matched neighbours (type, direction and content). Every other rename is reported as one element removed and one added. For example, an isolated class renamed from Shelf to Rack is reported as removed and added.
    • Edges are matched by id, otherwise by type and (matched) ends, preferring the same content. A matched edge keeps its id and handles, and keeps its bend points unless one of its ends grew. Then points is cleared and the editor routes the edge again.
    • A new top-level node goes on the first free slot next to a matched neighbour it is connected to, otherwise to the right of the diagram. If nothing matched, the imported layout is left as it is.
  • POST /besser_api/get-json-model takes an optional form field existing_model. When it is present, the imported diagram goes through adopt_existing_layout. Without it the response is the same as before. This also applies when the uploaded file is a project and the endpoint picks one diagram from it.
  • Partial-import warning: agent_buml_to_json(content, warnings=None) now reports what it could not read: the whole-file fallback, plus bodies and statements it skipped. /get-json-model returns these as buml-import-partial warnings. An agent written with keyword arguments (agent.new_intent(name=...)) used to import as an empty diagram with HTTP 200 and no warning. It still imports empty, but the response now says why.
  • The response shape is unchanged: DiagramExportResponse.warnings ([{code, message, ...}]) already existed. The new codes add structured keys.

Diagram types covered

  • Class and Agent: covered, including through the endpoint.
  • State Machine: same mechanism, covered at the function level (JSON → B-UML → JSON round trip). Through /get-json-model, a state-machine .py is still detected as an agent (.new_state( is an agent marker; that is finding 2 of the analysis and out of scope here). With an existing_model of type StateMachineDiagram, the endpoint answers layout-not-adopted ("the existing diagram is a StateMachineDiagram, the file holds a AgentDiagram") and does not adopt the layout silently.
  • Other types: existing_model is ignored with a layout-not-adopted warning. BPMN, Component and Deployment diagrams already keep their layout through B-UML.
  • /get-project-json-model: not changed. It builds a new project through project_to_json, which is a different code path.

Warning codes

code when extra keys
layout-adopted existing_model was used matched, edgesKept, edgesRerouted, edgesAdded
element-renamed rename recognised elementId, type, from, name
element-added new top-level element placed elementId, type, name, placedNextTo (name or null)
element-not-in-import existing element missing from the file elementId, type, name
layout-not-adopted existing_model not JSON, not v4 (nodes), another diagram type, or a type without adoption —
buml-import-partial the agent parser skipped part of the file —

Tests

  • tests/.../services/converters/test_layout_adoption.py (11 tests): unchanged agent and unchanged class keep every node, edge, row and component id and position; a new state is placed next to its neighbour without overlap; a rename with the same connections keeps its place; an isolated rename is reported as removed and added; a removed state is reported; same-named nodes are paired in order and no id is used twice; an edge drops its bend points when an end grew; a state machine round trip keeps its layout; an unusable existing_model (invalid JSON, v3, another type, unsupported type) is ignored with a warning; an empty existing diagram leaves the imported layout as it is.
  • tests/.../backend/test_layout_preserving_import.py (4 endpoint tests, using the backend's client fixture): end-to-end re-import with existing_model; no existing_model gives the old response; invalid existing_model gives a warning; a keyword-style agent gives buml-import-partial.

Commands, run serially in a venv with pip install -e .[dev], the backend requirements and .[django] (pytest-xdist collected different test ids per worker here, so I did not use -n auto):

python -m pytest tests/utilities/web_modeling_editor/backend/test_layout_preserving_import.py tests/utilities/web_modeling_editor/backend/services/converters/test_layout_adoption.py -q
15 passed in 0.25s
python -m pytest tests/utilities -q              -> 1 failed, 5084 passed, 6 skipped
python -m pytest tests/BUML tests/docs tests/llm tests/workflows tests/live -q   -> 1581 passed, 8 skipped
python -m pytest tests/generators -q             -> 2 failed, 1218 passed, 76 skipped, 2 xfailed
python -m pytest tests/spec_driven_agent -q      -> 3877 passed, 35 skipped
(11890 collected in total)
ruff check besser/   -> All checks passed!
lint-imports         -> Contracts: 4 kept, 0 broken.

The three failures are unrelated to this change:

  • test_secret_redaction_callers.py::test_the_agent_transform_endpoint_logs_its_config_redacted posts a v3 agent model, which this branch rejects with HTTP 400 before the log line it checks. This PR does not touch that endpoint.
  • The two test_derived_enum_entity_can_be_created failures come from the local host: bubblewrap cannot create namespaces there ("the shell sandbox is unavailable on this server").

The docs build (docs/check_docs.py) could not run in my sandbox, because sphinx_immaterial starts a multiprocessing manager. The edited RST section parses cleanly with docutils. .github/scripts/check_openapi.py will probably report the editor's committed OpenAPI schema as stale: the endpoint gained a form field and its docstring changed. The editor PR has to regenerate the schema (see below).

Editor follow-up

What the React Flow editor (BESSER-Web-Modeling-Editor) has to do in a second PR:

  1. Action: next to "Import diagram into project" (features/import/useImportDiagram.ts, which adds a new tab), add "Update current diagram from file (.py)". The current behaviour stays the default.
  2. Request: POST /besser_api/get-json-model, multipart, with buml_file (the .py file, as today) and existing_model, set to JSON.stringify(currentDiagram.model). That is the v4 model of the diagram being updated, with type, nodes, edges and, for agents, components. A { model: ... } project-diagram entry is accepted too. Send existing_model only for this action and only when the current diagram is a ClassDiagram, AgentDiagram or StateMachineDiagram. For the plain "import as new tab" action, do not send it. Starlette caps a form text field at 1 MB: an agent whose components[*].guiModel is large can be sent without guiModel, since only ids, types, names, positions, sizes, rows, components and edges are read.
  3. Response: the same DiagramExportResponse. Replace the current diagram's model with response.model in place, keeping the diagram id, title and envelope (config, references). Matched elements already carry their old ids, so cross-diagram references and the GitHub three-way merge keep working.
  4. Report before applying: read response.warnings. Show element-added (with placedNextTo), element-renamed and element-not-in-import: the update deletes what the file no longer has, so the user confirms first. Show layout-not-adopted (the diagram would get a new layout) and buml-import-partial (content was not read) as warnings. Unknown codes keep going to the existing toast path (reportImportRepairs.ts).
  5. Types: run npm run api:generate in packages/webapp and commit openapi/besser-openapi.json, then bump the submodule pointer here. Add the new warning codes to shared/api/conversion.ts.
  6. Docs: projects.rst, on what "update from file" does with a diagram of the same identity. Also i18n strings for the action and the report.

Other follow-ups that were not done here: state-machine detection in /get-json-model (finding 2), the agent re-import content differences (finding 3), and buml-import-partial on the project import path.

Handover

  • Entry points: adopt_layout / adopt_existing_layout in services/converters/layout_adoption.py; the router change is in get_single_json_model.
  • Known limits: a node that moved to another parent keeps its imported position. A ClassLinkRel edge, which is anchored on another edge, is matched by its ends, so it gets a new id when its target edge was remapped. Only new top-level nodes are placed; new child nodes keep the position the converter gave them inside their parent.

/get-json-model takes an optional existing_model (the diagram the file
updates). The imported Class, Agent or State Machine diagram is then laid
out like it: matched elements keep their id, position and size, new ones
are placed next to a neighbour, and warnings report what was kept, added
and missing. The agent converter now reports the parts of a file it could
not read (buml-import-partial) instead of only logging them.

Refs #774
@ArmenSl
ArmenSl marked this pull request as ready for review November 10, 2026 16:36
@ArmenSl
ArmenSl merged commit a67b356 into feature/migration-react-flow-sync Nov 12, 2026
@ArmenSl
ArmenSl deleted the fix/774-layout-preserving-import branch November 12, 2026 11:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant